package com.googlecode.aleverr.yafotkiclient.api;

import android.util.Log;
import org.w3c.dom.Document;

import javax.xml.xpath.*;
import java.io.IOException;

public class UserHome {

    private static final String TAG = "UserHome";

    private static final String ME_URL = "http://api-fotki.yandex.ru/api/me/";
    private static final String USER_HOME_FORMAT = "http://api-fotki.yandex.ru/api/users/%s/";

    private static final XPathExpression ALBUMS_EXPRESSION;
    private static final XPathExpression PHOTOS_EXPRESSION;
    private static final XPathExpression TAGS_EXPRESSION;

    static {
        XPath xPath = XPathFactory.newInstance().newXPath();
        XPathExpression expression = null;
        try {
            expression = xPath.compile("/service/workspace/collection[@id='album-list']/@href");
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        } finally {
            ALBUMS_EXPRESSION = expression;
        }
        expression = null;
        try {
            expression = xPath.compile("/service/workspace/collection[@id='photo-list']/@href");
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        } finally {
            PHOTOS_EXPRESSION = expression;
        }
        expression = null;
        try {
            expression = xPath.compile("/service/workspace/collection[@id='tag-list']/@href");
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        } finally {
            TAGS_EXPRESSION = expression;
        }
    }

    public static UserHome obtainUserHome(String userName, String accessToken) {
        String address;
        if (userName != null && !"".equals(userName)) {
            address = String.format(USER_HOME_FORMAT, userName);
        } else {
            address = ME_URL;
        }
        Document document = Utils.obtainDocument(address, accessToken);
        UserHome result = new UserHome();
        try {
            result.albumCollectionAddress = (String) ALBUMS_EXPRESSION.evaluate(document, XPathConstants.STRING);
        } catch (XPathExpressionException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        try {
            result.photoCollectionAddress = (String) PHOTOS_EXPRESSION.evaluate(document, XPathConstants.STRING);
        } catch (XPathExpressionException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        try {
            result.tagList = (String) TAGS_EXPRESSION.evaluate(document, XPathConstants.STRING);
        } catch (XPathExpressionException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        return result;
    }

    private String albumCollectionAddress;
    private String photoCollectionAddress;
    private String tagList;

    private UserHome() {
    }

    public String getAlbumCollectionAddress() {
        return albumCollectionAddress;
    }

    public String getPhotoCollectionAddress() {
        return photoCollectionAddress;
    }

    public String getTagList() {
        return tagList;
    }

    public static void main(String[] args) throws IOException {
        UserHome userHome = obtainUserHome(null, "594250735702441f8adf35172aea8809");
        if (userHome != null) {
            System.out.println(userHome.getAlbumCollectionAddress());
            System.out.println(userHome.getPhotoCollectionAddress());
            System.out.println(userHome.getTagList());
        }
    }
}
